import sys

import command
import constants


def add_var(key, env_value):
    if command.is_windows():
        add_var_windows(key, env_value)
    else:
        print("not support")


# windows添加用户级别变量
def add_var_windows(key, env_value):
    if env_value is None:
        print("Please provide an environment")
        return
    result = command.exe_command_with_powershell(
        constants.WIN_MODIFY_ENVIRONMENT_VARIABLE.format(var_key=key, value=env_value))
    if result.returncode == 0:
        print("add env var Successful")
    else:
        print("add env var Failed")


# 获取用户级别变量
def get_temp_var(key):
    if command.is_windows():
        get_var_windows(key)
    else:
        print("not supported")


def get_var_windows(key):
    echo_var = constants.WIN_GET_ENVIRONMENT_VARIABLE.format(key)
    result = command.exe_command_with_powershell(echo_var)
    if result.returncode == 0:
        print(result.stdout.strip())
        return result.stdout.strip()
    else:
        print(result.stderr.strip())
        return None


if __name__ == '__main__':
    argv = sys.argv
    if len(argv) < 2:
        print("Please provide an environment key")
    else:
        get_temp_var(argv[1])
